import service from './request'
import md5 from 'js-md5'
import { useFlockInfoStore } from '@/stores/flockInfo'
import { useBaseParamStore } from '@/stores/baseParams'

const cqBaseUrl = import.meta.env.VITE_CQ_SERVER_URL
const accountId = import.meta.env.VITE_CQ_ACCOUNT_ID
const salt = import.meta.env.VITE_CQ_LOGIN_SALT
const headers = { 'Content-Type': 'application/json' }

// 上传图片
export const uploadImage = (file) => {
  console.log('file', file)
  const formData = new FormData()
  formData.append('file', file)

  // return service.post(cqBaseUrl + '/wens/auth/file/uploadImage.do', formData, {
  //   headers: { 'Content-Type': 'multipart/form-data' }
  // })
  return service.request({
    url: cqBaseUrl + '/wens/auth/file/uploadImage.do',
    method: 'post',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}

// 获取用户列表
export const getUserList = (data) => {
  const size = 100;
  const current = 1;
  const { keyword, partOrg, orgId } = data;
  const params = new URLSearchParams({ keyword, size, current, partOrg, orgId }).toString();

  return service.get(
    cqBaseUrl + `/kapi/v2/wens/wens_smarb_aivideo/user/list?${params}`,
    {
      headers: headers
    });
}

// 获取类型树
export const getTypeTree = () => {
  return service.get(
    cqBaseUrl + `/kapi/v2/wens/wens_area_digital_space/patrolAlarm/tree`,
    {
      headers: headers
    });
}

// 获取类型列表
export const getTypeList = (data) => {
  const { keyword, parentId } = data;
  const params = new URLSearchParams({ keyword, parentId }).toString();
  return service.get(
    cqBaseUrl + `/kapi/v2/wens/wens_area_digital_space/patrolAlarm/list?${params}`,
    {
      headers: headers
    });
}

// 保存远程点检内容接口
export const savePatrolAlarmAndTask = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/savePatrolAlarmAndTask',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 获取组织树
export const SbLeftTreeByEntity = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/app/wens_smab_common/SbLeftTreeByEntity',
    {
      ...params
    },
    {
      headers: headers,
      params: getTokenParams()
    }
  )
}

// 获取养户列表
export const getRearerListByOrgId = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getRearerListByOrgId',
    {
      ...params
    },
    {
      headers: headers,
      params: getTokenParams()
    }
  )
}

// 养户信息
export const getRearerInfo = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getRearerInfo',
    {
      ...params
    },
    {
      headers: headers
      // params: getTokenParams()
    }
  )
}

// 获取告警详情
export const getDeviceInfo = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getDeviceInfo',
    {
      ...params
    },
    {
      headers: headers
      // params: getTokenParams()
    }
  )
}

// 获取服务部告警列表
export const getPatrolAlarmByOrg = (params) => {
  return service.get(
    cqBaseUrl +
    `/kapi/v2/wens/wens_area_digital_space/wens_patrol_alarm/getPatrolAlarmByOrg?orgId=${params.orgId}&pageSize=${params.pageSize}&pageNo=${params.pageNo}`,
    {
      headers: headers
      // params: getTokenParams()
    }
  )
}

// 获取根据养户id 获取任务告警信息列表
export const getPatrolAlarmByRearerV2 = (params) => {
  return service.get(
    cqBaseUrl +
    `/kapi/v2/wens/wens_area_digital_space/wens_patrol_task/getPatrolAlarmByRearerV2?rearerId=${encodeURIComponent(params.rearerId)}&pageSize=${params.pageSize}&pageNo=${params.pageNo}`,
    {
      headers: headers
      // params: getTokenParams()
    }
  )
}

// 获取根据养户id 获取告警信息列表
export const getPatrolAlarmByRearer = (params) => {
  return service.get(
    cqBaseUrl +
    `/kapi/v2/wens/wens_area_digital_space/wens_patrol_alarm/getPatrolAlarmByRearer?rearerId=${encodeURIComponent(params.rearerId)}&pageSize=${params.pageSize}&pageNo=${params.pageNo}`,
    {
      headers: headers
    }
  )
}

// 巡查计划列表
export const getDigitalSpaceByOrg = (params) => {
  return service.get(
    cqBaseUrl +
    `/kapi/v2/wens/wens_fatten_operation/wens_inspection_plans/getDigitalSpaceByOrg?orgId=${params.orgId}&pageSize=${params.pageSize}&pageNo=${params.pageNo}`,
    {
      headers: headers
      // params: getTokenParams()
    }
  )
}

// 根据养户id获取巡查计划列表
export const getInspectionPlansByRearer = (params) => {
  return service.get(
    cqBaseUrl +
    `/kapi/v2/wens/wens_fatten_operation/wens_inspection_plans/getInspectionPlansByRearer?rearerId=${encodeURIComponent(params.rearerId)}&pageSize=${params.pageSize}&pageNo=${params.pageNo}`,
    {
      headers: headers
      // params: getTokenParams()
    }
  )
}

// 获取任务列表
export const getPatrolTaskListByOrgId = (params) => {
  return service.get(
    cqBaseUrl +
    `/kapi/v2/wens/wens_area_digital_space/wens_patrol_task/getPatrolTaskListByOrgId?orgId=${params.orgId}&pageSize=${params.pageSize}&pageNo=${params.pageNo}`,
    {
      headers: headers
      // params: getTokenParams()
    }
  )
}

// 根据养户id获取任务列表
export const getPatrolTaskByRearer = (params) => {
  return service.get(
    cqBaseUrl +
    `/kapi/v2/wens/wens_area_digital_space/wens_patrol_task/getPatrolTaskByRearer?rearerId=${encodeURIComponent(params.rearerId)}&pageSize=${params.pageSize}&pageNo=${params.pageNo}`,
    {
      headers: headers
      // params: getTokenParams()
    }
  )
}

// 获取死淘数据
export const getDeadOutInfo = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getDeadOutInfo',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 获取猪群信息
export const getFlockInfo = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getFlockInfo',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 获取生物资产
export const getBiologicalAssetsInfo = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getBiologicalAssetsInfo',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 获取异常告警统计
export const getAlarmStatisticsInfo = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getAlarmStatisticsInfo',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 现场巡查统计
export const getAlarmTaskInfo = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolTask/getAlarmTaskInfo',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 获取告警核实信息
export const getPatrolAlarmDetailInfo = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getPatrolAlarmDetailInfo',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 任务中心获取告警接口
export const getPatrolAlarmDetail = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/getPatrolAlarmDetail',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 任务中心获取预分配人员接口
export const getPatrolUse = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolUser/getPatrolUser',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 任务中心获取派单权限接口
export const getRolePermissions = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolUser/getRolePermissions',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 任务中心派单接口
export const setDispatchTask = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolTask/setDispatchTask',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 保存巡查内容接口
export const savePatrolAlarmDetails = (params) => {
  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_area_digital_space/patrolAlarm/savePatrolAlarmDetails',
    {
      ...params
    },
    {
      headers: headers
    }
  )
}

// 配置页面参数存储
const getBaseParam = () => {
  return useBaseParamStore().getBaseParam.value
}

// 苍穹老接口token放params里
const getTokenParams = () => {
  let param = getBaseParam()
  return {
    access_token: param.cqToken
  }
}

export const getCountMsgByFlk = (flockIdStr) => {
  let param = getBaseParam()

  return service.post(
    cqBaseUrl + '/kapi/app/wens_count_pig/SbCountInfoByFlk',
    {
      flockid: flockIdStr,
      businessType: param.businessType,
      status: '0'
    },
    {
      headers: headers,
      params: getTokenParams()
    }
  )
}

// 更新点数信息
export const updateCountMsg = () => {
  let info = getFlockInfo()
  info.status = 0

  return service.post(cqBaseUrl + '/kapi/app/wens_count_pig/SbCountInfoPostFlk', info, {
    headers: headers,
    params: getTokenParams()
  })
}

// 完成点数，插入点数信息
export const insertCountMsg = () => {
  let info = getFlockInfo()
  info.status = 1

  return service.post(cqBaseUrl + '/kapi/app/wens_count_pig/SbCountInfoPostFlk', info, {
    headers: headers,
    params: getTokenParams()
  })
}

// 获取人员列表
export const getPeopleList = (name, page, size) => {
  return service.post(
    cqBaseUrl + '/kapi/app/wens_bio_safety/getPersonPage',
    { name: name, page: page, size: size },
    {
      headers: headers,
      params: getTokenParams()
    }
  )
}

// 登录
// export const login = () => {
//   let param = getBaseParam()
//   let passwordStr = md5(param.easPersonId + salt)

//   return service.post(
//     cqBaseUrl + '/kapi/app/wens_smab_common/SbEaspersonLogin',
//     {
//       accountId: accountId,
//       easPersonId: param.easPersonId,
//       password: passwordStr
//     },
//     {
//       headers: headers
//     }
//   )
// }

// sit 登录信息
// accountId: '2159611359669524480',
// logintype: '2',
// password: '1234567', //密码
// tenantid: 'wens',
// user: '18122460771' // 账号

// dev 登录信息
// accountId: '1558431403615522905',
// logintype: '2',
// password: 'admin123', //密码
// tenantid: 'wens',
// user: '13480374218' // 账号

// uat信息
// accountId: '723210654470635520',
// logintype: '2',
// password: '1234567', //密码
// tenantid: 'wens',
// user: '15907668393' // 账号
export const login = () => {
  return service({
    method: 'post',
    url: cqBaseUrl + '/api/login.do',
    data: {
      accountId: '723210654470635520',
      logintype: '2',
      password: '1234567', //密码
      tenantid: 'wens',
      user: '15907668393' // 账号
    }
  })
}

// 上传临时文件到苍穹
export const uploadTempFileToCq = (file) => {
  file.file

  let formData = new FormData()
  formData.append('file', file)

  return service({
    method: 'post',
    url: cqBaseUrl + '/attachment/uploadFile.do',
    params: getTokenParams(),
    data: formData
  })
}

// 点猪算法接口
const appId = import.meta.env.VITE_CQ_ALGO_APP_ID
const appSecret = import.meta.env.VITE_CQ_ALGO_APP_SECRET
export const pigCountAlgo = (fileUrl, isAddToken) => {
  if (isAddToken) {
    let param = getBaseParam()
    fileUrl = fileUrl + '&access_token=' + encodeURIComponent(param.cqToken)
  }

  return service.post(
    cqBaseUrl + '/kapi/v2/wens/wens_algorithm/PigNumberAlgorithm',
    {
      pigNumberParamDto: {
        appId: appId,
        appSecret: appSecret,
        picParamList: [
          {
            picId: '',
            picUrl: fileUrl
          }
        ]
      }
    },
    {
      headers: headers
    }
  )
}

export const pigCountAlgoNew = (picBase64, score) => {
  return service.post(cqBaseUrl + '/kapi/v2/wens/wens_algorithm/middleApi', {
    apiParam: {
      apiCode: 'JK-241202-82C2BCE8',
      appId: appId,
      appSecret: appSecret,
      body: {
        img: picBase64,
        score: score
      }
    }
  })

  // let data = {}
  // data = JSON.parse(
  //   '{"data":{"msg":"success","code":0,"data":[[320,291,358,393,0.9147786498069763],[71,636,138,718,0.9073175191879272],[316,573,353,681,0.9050484299659729],[459,348,488,464,0.8925756216049194],[577,717,636,788,0.880866527557373],[187,666,236,742,0.8755526542663574],[627,274,666,364,0.8750462532043457],[248,676,325,730,0.873577892780304],[96,770,140,840,0.8694756627082825],[39,343,80,413,0.8653417229652405],[525,730,581,794,0.842122495174408],[73,125,122,198,0.8383299708366394],[89,720,120,778,0.833224892616272],[46,762,101,792,0.8284903168678284],[213,743,295,767,0.8252190947532654],[43,276,87,352,0.8098084330558777],[221,633,254,738,0.8066777586936951],[57,788,85,838,0.8044182658195496],[122,801,173,846,0.802112340927124],[56,114,97,174,0.7965770363807678],[81,876,126,899,0.7891109585762024],[139,865,167,918,0.7889841198921204],[58,717,90,763,0.7863919138908386],[551,800,608,853,0.7653992176055908],[50,654,77,707,0.7518298625946045],[28,504,97,543,0.7516044974327087],[91,910,144,939,0.7503319382667542],[39,518,121,564,0.7495144605636597],[53,281,107,321,0.7474730014801025],[509,756,556,812,0.7323393225669861],[598,829,651,895,0.7227063179016113],[42,825,98,861,0.7028947472572327],[83,840,136,867,0.6776381134986877],[62,900,90,925,0.6746907830238342],[584,879,631,923,0.6600444316864014],[45,859,97,885,0.6345772743225098],[34,815,55,857,0.5583320260047913],[24,481,75,546,0.4906555116176605],[60,882,82,904,0.464609295129776],[28,482,75,516,0.4489545524120331]]},"errorCode":"0","message":null,"status":true}'
  // )
  // return new Promise((resolve) => {
  //   resolve(data)
  // })
}

// test /mapi/wens/auth/api/invoke.do?svc=wsmall.admindivisionQuery
export const test = () => {
  return service.post(
    cqBaseUrl + '/wens/auth/api/invoke.do?svc=wsmall.admindivisionQuery',
    {
      comeFrom: 'mobile',
      method: 'admindivisionQuery',
      data: { level: '001' }
    },
    {
      headers: headers
    }
  )
}
